<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
      title="解决方案经理"
      :visible.sync="show"
      append-to-body
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="150px"
      >
        <el-form-item label="解决方案经理" prop="solutionManager">
          <el-select
            v-model="formData.solutionManager"
            placeholder="请选择解决方案经理"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in solutionManagerOptions"
              :key="index"
              :label="item.userName"
              :value="item.userId"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="解行业BU" prop="tradeBu">
          <el-select
            v-model="formData.tradeBu"
            placeholder="请选择行业BU"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in tradeBuOptions"
              :key="index"
              :label="item.userName"
              :value="item.userId"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="区县营服主任" prop="areaDirector">
          <el-select
            v-model="formData.areaDirector"
            placeholder="请选择区营服主任"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in areaDirectorOptions"
              :key="index"
              :label="item.userName"
              :value="item.userId"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close" size="mini">取消</el-button>
        <el-button type="primary" @click="handleConfirm" size="mini"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listUserRoleId } from "@/api/system/user";
import { assignBusiness, assignBusinessBu } from "@/api/business/index";
export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      formData: {
        solutionManager: undefined,
        areaDirector: undefined,
        tradeBu: undefined,
      },
      boId: "",
      rules: {
        tradeBu: [
          {
            required: true,
            message: "请选择行业BU",
            trigger: "change",
          },
        ],
        solutionManager: [
          {
            required: true,
            message: "请选择解决方案经理",
            trigger: "change",
          },
        ],
        areaDirector: [
          {
            required: true,
            message: "请选择区营服主任",
            trigger: "change",
          },
        ],
      },
      show: false,
      areaDirectorOptions: [],
      solutionManagerOptions: [],
      tradeBuOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {
    // this.loadUser();
  },
  mounted() {},
  methods: {
    onOpen() {},
    loadUser() {
      // roleId: 103 解决方案经理Id
      listUserRoleId({ pageNum: 1, pageSize: 9999, roleId: 103 }).then(
        (res) => {
          this.solutionManagerOptions = res.rows;
        }
      );

      // 行业bu
      listUserRoleId({ pageNum: 1, pageSize: 9999, roleId: 100 }).then(
        (res) => {
          this.tradeBuOptions = res.rows;
        }
      );

      // 区营服主任
      listUserRoleId({ pageNum: 1, pageSize: 9999, roleId: 102 }).then(
        (res) => {
          this.areaDirectorOptions = res.rows;
        }
      );
    },
    showDialog(row) {
      this.show = true;
      this.loadUser();
      console.log(row);
      this.boId = row.id;
    },
    onClose() {
      this.show = false;

      this.$refs["elForm"].resetFields();
    },
    close() {
      this.show = false;
      this.$emit("update:visible", false);
    },
    handleConfirm() {
      this.$refs["elForm"].validate(async (valid) => {
        if (!valid) return;
        await assignBusinessBu({
          areaDirector: this.formData.areaDirector,
          tradeBu: this.formData.tradeBu,
          boId: this.boId,
        });
        await assignBusiness({
          solutionManager: this.formData.solutionManager,
          boId: this.boId,
        });
        this.$message({
          message: "操作成功",
          type: "success",
        });
        this.$emit("success");
        this.close();
      });
    },
  },
};
</script>
<style></style>
